package cas.ihep.pwa.evtgen;

import java.io.Serializable;
import java.util.ArrayList;

public class EvtParticleDecayList implements Serializable {

    private ArrayList<EvtParticleDecay> _decaylist;
    double _rawbrfrsum;

    public EvtParticleDecayList(){
        _decaylist=null;
        _rawbrfrsum=0;
    }

    EvtParticleDecay getDecay(int nchannel){
        return _decaylist.get(nchannel);
    }

    public int getNMode(){
        return _decaylist==null?0:_decaylist.size();
    }

    void addMode(EvtDecayBase decay,double brfrsum,double massmin){
        if (_decaylist==null){
            _decaylist=new ArrayList<>();
        }
        _rawbrfrsum=brfrsum;
        EvtParticleDecay newdecay=new EvtParticleDecay();
        newdecay.setDecayModel(decay);
        newdecay.setBrfrSum(brfrsum);
        newdecay.setMassMin(massmin);
        _decaylist.add(newdecay);
    }

    public void removeDecay(){
        _decaylist=null;
        _rawbrfrsum=0;
    }

    void makeChargeConj(EvtParticleDecayList conjDecayList){
        _rawbrfrsum=conjDecayList._rawbrfrsum;
        _decaylist=new ArrayList<>();
        for(EvtParticleDecay other:conjDecayList._decaylist){
            EvtParticleDecay tt=new EvtParticleDecay();
            tt.chargeConj(other);
            _decaylist.add(tt);
        }
    }

    void finish(){
        if(_decaylist.size()>0){
            if(_rawbrfrsum<0.000001){
                throw new RuntimeException();
            }
            if(Math.abs(_rawbrfrsum-1)>0.0001){
                System.err.println("Warning");
            }
            for(EvtParticleDecay dd:_decaylist){
                double brfrsum=dd.getBrfrSum()/_rawbrfrsum;
                dd.setBrfrSum(brfrsum);
            }
        }
    }
}
